<?php
/**
 * Created by.
 * User: littleU-569598569
 * Date: 2021/3/15 0015
 * Time: 21:33
 */

namespace app\api\service;

use app\api\model\ProductCategory as ProductCategoryModel;


class ProductCategory extends BaseService
{
    public function getProductCategoryByCondition($data)
    {
        $condition = [];
        $page = isset($data['page']) ? $data['page'] : $this->page;
        $limit = isset($data['limit']) ? $data['limit'] : $this->limit;
        $order = isset($data['order']) ? $data['order'] : $this->order;
        if (isset($data['status'])) {
            $condition['status'] = $data['status'];
        }
        if (isset($data['name'])) {
            $condition['name'] = ['like', '%' . $data['name'] . '%'];
        }
        $result = (new ProductCategoryModel())->where($condition)->page($page)->limit($limit)->order($order)->select();
        $count = (new ProductCategoryModel())->where($condition)->count();

        return json([
            'code' => config('code.success'),
            'msg' => '请求成功',
            'data' => $result,
            'count' => $count
        ]);
    }

    public static function getNormalList()
    {
        $condition = [];
        $condition['status'] = config('code.normal');
        $result = (new ProductCategoryModel())->where($condition)->select();
        return json([
            'code' => config('code.success'),
            'data' => $result
        ]);
    }

    public static function setProductCategory($data)
    {
        $id = $data['id'];
        if($id != '')
        {
            //更新
            try
            {
                $model = new ProductCategoryModel();
                $model->allowField(true)->save($data, ['id' => $id]);
            }
            catch (\Exception $ex)
            {
                return json([
                    'code' => config('code.error'),
                    'msg'  => $ex->getMessage()
                ]);
            }

        }
        else
        {
            //新增
            try
            {
                $model = new ProductCategoryModel();
                $model->allowField(true)->save($data);
            }
            catch (\Exception $ex)
            {
                return json([
                    'code' => config('code.error'),
                    'msg'  => $ex->getMessage()
                ]);
            }
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '操作成功'
        ]);
    }

    public static function editProductCategory($data)
    {
        $condition = $data['data'];
        $field = $data['field'];
        try
        {
            $obj = ProductCategoryModel::get($condition['id']);
            $obj->$field = $condition[$field];
            $obj->save();
        }
        catch (\Exception $ex)
        {
            return json([
                'code' => config('code.error'),
                'msg'  => $ex->getMessage()
            ]);
        }
        return json([
            'code' => config('code.success'),
            'msg'  => '编辑成功'
        ]);
    }
}